/*
 * 
 * 
 * 
 */
package com.hboxs.asl.entity;

import com.hboxs.asl.ProductFieldBridge;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.FieldBridge;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Store;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Entity - 发货单
 */
@Entity
@Table(name = "xx_shipping")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_shipping_sequence")
public class Shipping extends BaseEntity {

    private static final long serialVersionUID = -261737051893669935L;

    /**
     * 编号
     */
    private String sn;

    /**
     * 配送方式
     */
    private String shippingMethod;

    /**
     * 物流公司
     */
    private String deliveryCorp;

    /**
     * 物流公司网址
     */
    private String deliveryCorpUrl;

    /**
     * 物流公司代码
     */
    private String deliveryCorpCode;

    /**
     * 运单号
     */
    private String trackingNo;

    /**
     * 物流费用
     */
    private BigDecimal freight;

    /**
     * 收货人
     */
    private String consignee;

    /**
     * 地区
     */
    private String area;

    /**
     * 地址
     */
    private String address;

    /**
     * 邮编
     */
    private String zipCode;

    /**
     * 电话
     */
    private String phone;

    /**
     * 操作员
     */
    private String operator;

    /**
     * 备注
     */
    private String memo;

    /**
     * 订单
     */
    private Order order;

    /**
     * 发货项
     */
    private List<ShippingItem> shippingItems = new ArrayList<ShippingItem>();

    /**
    private String

    /**
     * 获取编号
     *
     * @return 编号
     */
    @Column(nullable = false, updatable = false, unique = true, length = 100)
    public String getSn() {
        return sn;
    }

    /**
     * 设置编号
     *
     * @param sn 编号
     */
    public void setSn(String sn) {
        this.sn = sn;
    }

    /**
     * 获取配送方式
     *
     * @return 配送方式
     */
    @NotEmpty
    @Column(nullable = false, updatable = false)
    public String getShippingMethod() {
        return shippingMethod;
    }

    /**
     * 设置配送方式
     *
     * @param shippingMethod 配送方式
     */
    public void setShippingMethod(String shippingMethod) {
        this.shippingMethod = shippingMethod;
    }

    /**
     * 获取物流公司
     *
     * @return 物流公司
     */
    @NotEmpty
    @Column(nullable = false, updatable = false)
    public String getDeliveryCorp() {
        return deliveryCorp;
    }

    /**
     * 设置物流公司
     *
     * @param deliveryCorp 物流公司
     */
    public void setDeliveryCorp(String deliveryCorp) {
        this.deliveryCorp = deliveryCorp;
    }

    /**
     * 获取物流公司网址
     *
     * @return 物流公司网址
     */
    public String getDeliveryCorpUrl() {
        return deliveryCorpUrl;
    }

    /**
     * 设置物流公司网址
     *
     * @param deliveryCorpUrl 物流公司网址
     */
    @Column(updatable = false)
    public void setDeliveryCorpUrl(String deliveryCorpUrl) {
        this.deliveryCorpUrl = deliveryCorpUrl;
    }

    /**
     * 获取物流公司代码
     *
     * @return 物流公司代码
     */
    public String getDeliveryCorpCode() {
        return deliveryCorpCode;
    }

    /**
     * 设置物流公司代码
     *
     * @param deliveryCorpCode 物流公司代码
     */
    @Column(updatable = false)
    public void setDeliveryCorpCode(String deliveryCorpCode) {
        this.deliveryCorpCode = deliveryCorpCode;
    }

    /**
     * 获取运单号
     *
     * @return 运单号
     */
    @Length(max = 200)
    @Column(updatable = false)
    public String getTrackingNo() {
        return trackingNo;
    }

    /**
     * 设置运单号
     *
     * @param trackingNo 运单号
     */
    public void setTrackingNo(String trackingNo) {
        this.trackingNo = trackingNo;
    }

    /**
     * 获取物流费用
     *
     * @return 物流费用
     */
    @Min(0)
    @Digits(integer = 12, fraction = 3)
    @Column(updatable = false, precision = 21, scale = 2)
    public BigDecimal getFreight() {
        return freight;
    }

    /**
     * 设置物流费用
     *
     * @param freight 物流费用
     */
    public void setFreight(BigDecimal freight) {
        this.freight = freight;
    }

    /**
     * 获取收货人
     *
     * @return 收货人
     */
    @NotEmpty
    @Length(max = 200)
    @Column(nullable = false, updatable = false)
    public String getConsignee() {
        return consignee;
    }

    /**
     * 设置收货人
     *
     * @param consignee 收货人
     */
    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }

    /**
     * 获取地区
     *
     * @return 地区
     */
    @NotEmpty
    @Column(nullable = false, updatable = false)
    public String getArea() {
        return area;
    }

    /**
     * 设置地区
     *
     * @param area 地区
     */
    public void setArea(String area) {
        this.area = area;
    }

    /**
     * 获取地址
     *
     * @return 地址
     */
    @NotEmpty
    @Length(max = 200)
    @Column(nullable = false, updatable = false)
    public String getAddress() {
        return address;
    }

    /**
     * 设置地址
     *
     * @param address 地址
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * 获取邮编
     *
     * @return 邮编
     */
    @NotEmpty
    @Length(max = 200)
    @Column(nullable = false, updatable = false)
    public String getZipCode() {
        return zipCode;
    }

    /**
     * 设置邮编
     *
     * @param zipCode 邮编
     */
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    /**
     * 获取电话
     *
     * @return 电话
     */
    @NotEmpty
    @Length(max = 200)
    @Column(nullable = false, updatable = false)
    public String getPhone() {
        return phone;
    }

    /**
     * 设置电话
     *
     * @param phone 电话
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * 获取操作员
     *
     * @return 操作员
     */
    @Column(nullable = false, updatable = false)
    public String getOperator() {
        return operator;
    }

    /**
     * 设置操作员
     *
     * @param operator 操作员
     */
    public void setOperator(String operator) {
        this.operator = operator;
    }

    /**
     * 获取备注
     *
     * @return 备注
     */
    @Length(max = 200)
    @Column(updatable = false)
    public String getMemo() {
        return memo;
    }

    /**
     * 设置备注
     *
     * @param memo 备注
     */
    public void setMemo(String memo) {
        this.memo = memo;
    }

    /**
     * 获取订单
     *
     * @return 订单
     */
    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "orders", nullable = false, updatable = false)
    public Order getOrder() {
        return order;
    }

    /**
     * 设置订单
     *
     * @param order 订单
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * 获取发货项
     *
     * @return 发货项
     */
    @Valid
    @NotEmpty
    @OneToMany(mappedBy = "shipping", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public List<ShippingItem> getShippingItems() {
        return shippingItems;
    }

    /**
     * 设置发货项
     *
     * @param shippingItems 发货项
     */
    public void setShippingItems(List<ShippingItem> shippingItems) {
        this.shippingItems = shippingItems;
    }

    /**
     * 获取数量
     *
     * @return 数量
     */
    @Transient
    public int getQuantity() {
        int quantity = 0;
        if (getShippingItems() != null) {
            for (ShippingItem shippingItem : getShippingItems()) {
                if (shippingItem != null && shippingItem.getQuantity() != null) {
                    quantity += shippingItem.getQuantity();
                }
            }
        }
        return quantity;
    }

}